#include "LLConverter.h"
#include <cmath>

void LLConverter::Init(float64 Lat0, float64 Lon0)
{
	// params
	float64 f = 1/298.257223563; // flattening
	float64 R = 6378137; // radius of planet

	m_Lat0 = Lat0;
	m_Lon0 = Lon0;

	float64 sinLat0 = sin(Lat0*3.1415926535/180);
	m_cosLat0 = cos(Lat0*3.1415926535/180);

	m_RN = R / sqrt(1-(2*f-f*f) * pow(sinLat0, 2));
	m_RM = m_RN*(1-(2*f-f*f)) / (1-(2*f-f*f) * pow(sinLat0, 2));

	m_KdN = 3.1415926535/180 / atan(1/m_RM);
	m_KdE = 3.1415926535/180 / atan(1/(m_RN * m_cosLat0));
}

void LLConverter::LLToFlatEarth(float64 Lat, float64 Lon, float64& dN, float64& dE)
{
	float64 dLat = Lat-m_Lat0;
	float64 dLon = Lon-m_Lon0;

	dN = dLat * m_KdN;
	dE = dLon * m_KdE;
}
